package com.jsd2412.backend.turnBed.service.impl;

import com.jsd2412.backend.bed.mapper.BedMapper;
import com.jsd2412.backend.bed.pojo.dto.BedQuery;
import com.jsd2412.backend.bed.pojo.vo.BedVO;
import com.jsd2412.backend.room.mapper.RoomMapper;
import com.jsd2412.backend.room.pojo.dto.RoomQuery;
import com.jsd2412.backend.turnBed.mapper.TurnBedMapper;
import com.jsd2412.backend.turnBed.pojo.dto.TurnBedAddParam;
import com.jsd2412.backend.turnBed.pojo.dto.TurnBedQuery;
import com.jsd2412.backend.turnBed.pojo.entity.TurnBed;
import com.jsd2412.backend.turnBed.pojo.vo.TurnBedVO;
import com.jsd2412.backend.turnBed.service.TurnBedService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class TurnBedServiceImpl implements TurnBedService {
    @Autowired
    private TurnBedMapper turnBedMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private BedMapper bedMapper;

    @Override
    public void addTurnBed(TurnBedAddParam turnBedAddParam) {
        log.debug("添加转床记录业务:{}", turnBedAddParam);

        //获取房间id
        RoomQuery roomQuery = new RoomQuery();
        roomQuery.setNumber(turnBedAddParam.getRoomNumber());
        Integer roomId = roomMapper.select(roomQuery).get(0).getId();

        //获取床位id
        BedQuery bedQuery = new BedQuery();
        bedQuery.setNumber(turnBedAddParam.getBedNumber());
        bedQuery.setRoomId(roomId);
        Integer bedId = bedMapper.select(bedQuery).get(0).getBedId();//原床位id
        bedQuery.setNumber(turnBedAddParam.getNewBedNumber());
        Integer newBedId = bedMapper.select(bedQuery).get(0).getBedId();//新床位id

        //包装TurnBed类对象
        TurnBed turnBed = new TurnBed();
        turnBed.setPatientId(turnBedAddParam.getId());
        turnBed.setRoomId(roomId);
        turnBed.setOriginBedId(bedId);
        turnBed.setNewBedId(newBedId);
        turnBed.setCreateTime(new Date());
        turnBed.setReason(turnBedAddParam.getReason());

        turnBedMapper.insert(turnBed);

    }

    @Override
    public List<TurnBedVO> selectTurnBed(TurnBedQuery turnBedQuery) {
        log.debug("查询转床记录业务：{}", turnBedQuery);
        List<TurnBedVO> list = turnBedMapper.selectTurnBed(turnBedQuery);
        //填充缺失的信息
        for (TurnBedVO turnBedVO : list) {
            //获取房号
            String roomNumber = roomMapper.selectById(turnBedVO.getRoomId()).getNumber();
            turnBedVO.setRoomNumber(roomNumber);
            //获取原床位的床号和金额
            BedVO OriginBedVO = bedMapper.selectById(turnBedVO.getOriginBedId());
            if(OriginBedVO!=null){
                turnBedVO.setOriginBed(OriginBedVO.getNumber());
                turnBedVO.setOriginBedPrice(OriginBedVO.getPrice());
            }
            //获取新床位的床号和金额
            BedVO newBedVO = bedMapper.selectById(turnBedVO.getNewBedId());
            if(newBedVO!=null){
                turnBedVO.setNewBed(newBedVO.getNumber());
                turnBedVO.setNewBedPrice(newBedVO.getPrice());
            }
        }
        return list;
    }
}
